package com.cool.shark.controller;

import com.cool.shark.pojo.dto.AddAdminDto;
import com.cool.shark.pojo.dto.AdminDto;
import com.cool.shark.pojo.dto.AdminUpdateDto;
import com.cool.shark.pojo.entity.Admin;
import com.cool.shark.pojo.vo.AdminListItemVO;
import com.cool.shark.pojo.vo.AdminStandardVO;
import com.cool.shark.pojo.vo.ProductCategoryVo;
import com.cool.shark.service.AdminService;
import com.cool.shark.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Name: AdminController
 * @Author: 西风残叶
 * @Date: 2024/3/31 20:47
 * @Version: 1.0
 * @TODO: TODO
 */
@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @PostMapping("/addAdmin")
    public JsonResult<Void> addAdmin(@RequestBody AddAdminDto addAdminDto) {
        log.info("addAdminDto = {}", addAdminDto);
        log.debug("管理员注册");
        adminService.addAdmin(addAdminDto);
        return JsonResult.ok();
    }


    @PostMapping("/login")
    public JsonResult<Admin> login(@RequestBody AdminDto adminDto) {
        log.info("admin = {}", adminDto);
        log.debug("管理员登录");
        Admin login = adminService.login(adminDto);
        log.info("login = {}", login);
        return JsonResult.ok(login);
    }


    @GetMapping("/list")
    public JsonResult<List<AdminListItemVO>> list() {
        log.debug("管理员列表");
        // 调用adminService的list方法获取管理员列表，并返回其结果
        List<AdminListItemVO> list = adminService.list();
        return JsonResult.ok(list);
    }


    @GetMapping("/getProductCategory")
    public JsonResult<List<ProductCategoryVo>> getProductCategory() {
        log.debug("商品分类");
        // 从adminService获取商品分类列表
        List<ProductCategoryVo> list = adminService.getProductCategory();
        return JsonResult.ok(list);
    }
    @GetMapping("/getGoodsNum")
    public JsonResult<Integer> getGoodsNum() {
        log.debug("商品数量");
        // 从adminService获取商品数量
        int goodsCount = adminService.goodsCount();
        log.info("goodsCount = {}", goodsCount);
        return JsonResult.ok(goodsCount);
    }

    @GetMapping("/getAdminByUsername")
    public JsonResult<AdminStandardVO> getAdminByUsername(String username) {
        log.debug("根据用户名获取管理员");
        AdminStandardVO admin = adminService.getAdminByUsername(username);
        return JsonResult.ok(admin);
    }
    @PostMapping("/updateAdmin")
    public JsonResult<Void> updateAdmin(@RequestBody AdminUpdateDto adminUpdate) {
        log.debug("更新管理员信息,参数 :{}", adminUpdate);
        adminService.updateAdmin(adminUpdate);
        return JsonResult.ok();
    }
    @GetMapping("/delete")
    public JsonResult<Void> deleteAdmin(String username) {
        log.debug("删除管理员,参数 :{}", username);
        adminService.deleteAdmin(username);
        return JsonResult.ok();
    }
}